package com.itany.corejava.code12_线程;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年11月29日 13:49
 */
public class Test09_同步代码块 {
    public static void main(String[] args) {
        TicketOne t1=new TicketOne();
        TicketOne t2=new TicketOne();
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
    }
}


class TicketOne extends Thread{
    private static  int num=100;
    static Object object=new Object();
    @Override
    public void run() {
        while(true){
            //同步代码块
            // 锁对象是唯一的
            synchronized (object){
                if(num==0){
                    break;
                }else{
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"正在售票,当前第"+(num--));
                }
            }

        }
    }
}


